/**
 * jims
 */
package com.jims.master.modules.med.service;

import com.jims.master.common.config.OperType;
import com.jims.master.common.persistence.Page;
import com.jims.master.common.service.CrudService;
import com.jims.master.common.utils.ChineseCharToEnUtils;
import com.jims.master.common.utils.PrintPdfUtils;
import com.jims.master.modules.doctor.entity.PatMasterIndex;
import com.jims.master.modules.doctor.service.PatMasterIndexService;
import com.jims.master.modules.med.dao.MedicalCardMasterDao;
import com.jims.master.modules.med.entity.MedicalCardChangeLog;
import com.jims.master.modules.med.entity.MedicalCardConsumeMaster;
import com.jims.master.modules.med.entity.MedicalCardMaster;
import com.jims.master.modules.sys.dao.CompanyDao;
import com.jims.master.modules.sys.entity.User;
import com.jims.master.modules.sys.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 一卡通办理Service
 * @author 陈晓阳
 * @version 2017-05-22
 */
@Service
@Transactional(readOnly = true)
public class MedicalCardMasterService extends CrudService<MedicalCardMasterDao, MedicalCardMaster> {

    @Autowired
    private PatMasterIndexService patMasterIndexService;
    @Autowired
    private MedicalCardChangeLogService medicalCardChangeLogService;
    @Autowired
    private MedicalCardRechangeRecService medicalCardRechangeRecService;
    @Autowired
    private MedicalCardMasterDao medicalCardMasterDao;
    @Autowired
    private MedicalCardConsumeMasterService medicalCardConsumeMasterService;
    @Autowired
    private MedicalCardConsumeDetailService medicalCardConsumeDetailService;
    @Autowired
    private CompanyDao companyDao;


	public MedicalCardMaster get(String id) {
		return super.get(id);
	}
	
	public List<MedicalCardMaster> findList(MedicalCardMaster medicalCardMaster) {
		return super.findList(medicalCardMaster);
	}
	
	public Page<MedicalCardMaster> findPage(Page<MedicalCardMaster> page, MedicalCardMaster medicalCardMaster) {
		return super.findPage(page, medicalCardMaster);
	}

    /**
     * 一卡通保存
     * @param medicalCardMaster
     * @return
     */
	@Transactional(readOnly = false)
	public  Map<String,Object> save(MedicalCardMaster medicalCardMaster,Map<String,Object> resultVal) {
        int rows=0;
        if(medicalCardMaster.operType().equals(OperType.新建)){
            medicalCardMaster.setData(savePat(medicalCardMaster,true),null);
            rows+= super.save(medicalCardMaster);
            resultVal.put("balance",medicalCardMaster.getBalance());
            if(medicalCardMaster.isRechange()){
                rows+=medicalCardRechangeRecService.save(medicalCardMaster.getMedicalCardRechangeRec());
            }
        }else if(medicalCardMaster.operType().equals(OperType.修改)){
            medicalCardMaster.setData(null,patMasterIndexService.get(medicalCardMaster.getPatientId()));
            rows+=patMasterIndexService.save(medicalCardMaster.getPatMasterIndex());
        }else if(medicalCardMaster.operType().equals(OperType.充值)){
            medicalCardMaster.setData(null,null);
            MedicalCardMaster med=medicalCardMasterDao.findByCardNo(medicalCardMaster);
            medicalCardMaster.setBalance(med.getBalance()+medicalCardMaster.getDeposit());
            rows+=medicalCardMasterDao.updateBalance(medicalCardMaster);
            resultVal.put("balance",medicalCardMaster.getBalance());
            rows+=medicalCardRechangeRecService.save(medicalCardMaster.getMedicalCardRechangeRec());
        }else if (medicalCardMaster.operType().equals(OperType.补办)){
            MedicalCardMaster medicalCardMaster1=medicalCardMasterDao.findByIdCard(medicalCardMaster);
            medicalCardMaster.setData(null,null);
            rows+=super.save(medicalCardMaster);
            rows+=medicalCardMasterDao.delete(medicalCardMaster1);
        }else if(medicalCardMaster.operType().equals(OperType.退卡)){
            MedicalCardMaster medicalCardMaster1=medicalCardMaster=medicalCardMasterDao.get(medicalCardMaster.getId());
            medicalCardMaster1.setFlag("5");
            medicalCardMaster1.setData(null,null);
            rows+=medicalCardRechangeRecService.save(medicalCardMaster1.getMedicalCardRechangeRec());
            rows+=medicalCardMasterDao.delete(medicalCardMaster1);
        }
        resultVal.put("rows",rows);
        return resultVal;
	}


    /**
     * 保存病人信息
     * @param medicalCardMaster
     * @param isNew
     * @return
     */
    private String savePat(MedicalCardMaster medicalCardMaster,boolean isNew){
        String resultVal="";
        PatMasterIndex patMasterIndex=new PatMasterIndex();
        patMasterIndex.setName(medicalCardMaster.getName());
        patMasterIndex.setInputCode(ChineseCharToEnUtils.getAllFirstLetter(medicalCardMaster.getName()));
        patMasterIndex.setSex(medicalCardMaster.getSex());
        patMasterIndex.setAge(medicalCardMaster.getAge());
        patMasterIndex.setNation(medicalCardMaster.getNation());//
        patMasterIndex.setHomeplace(medicalCardMaster.getHomeplace());
        patMasterIndex.setIdNo(medicalCardMaster.getIdCard());
        patMasterIndex.setPhone(medicalCardMaster.getPhone());
        patMasterIndex.setChargeType(medicalCardMaster.getExpand1());
        patMasterIndexService.save(patMasterIndex);
        resultVal=patMasterIndex.getId();
        return resultVal;
    }




	@Transactional(readOnly = false)
	public int delete(MedicalCardMaster medicalCardMaster) {
		return super.delete(medicalCardMaster);
	}

    /**
     * 根据卡号查询信息
     * @param medicalCardMaster
     * @return
     */
    public MedicalCardMaster findByCardNo(MedicalCardMaster medicalCardMaster) {
        return medicalCardMasterDao.findByCardNo(medicalCardMaster);
    }

    @Transactional(readOnly = false)
    public int update(MedicalCardMaster medicalCardMaster) {
        int resultVal=0;
        MedicalCardChangeLog medicalCardChangeLog=new MedicalCardChangeLog();
        medicalCardChangeLog.setCardNo(medicalCardMaster.getCardNo());
        medicalCardChangeLog.setReason("1".equals(medicalCardMaster.getCardStatus())?"挂失":"启用");
        medicalCardChangeLog.setCardFlag("1".equals(medicalCardMaster.getCardStatus())?"2":"3");
        medicalCardChangeLog.setPatientId(medicalCardMaster.getPatientId());
        resultVal+=medicalCardChangeLogService.save(medicalCardChangeLog);
        resultVal+=medicalCardMasterDao.update(medicalCardMaster);
        return resultVal;
    }

    /**
     * 根据身份证读取病人挂失的卡信息
     * @param medicalCardMaster
     * @return
     */
    public MedicalCardMaster findByIdCard(MedicalCardMaster medicalCardMaster) {
        return medicalCardMasterDao.findByIdCard(medicalCardMaster);
    }

    /**
     * @param cardNo
     * @return
     */
    @Transactional(readOnly = false)
    public int returnFee(String cardNo,String charge) {
        return medicalCardMasterDao.returnFee(cardNo, charge);
    }
    /**
     *
     * @param feeId
     * @return
     */
    public MedicalCardConsumeMaster getByfeeId(String feeId) {
        return medicalCardConsumeMasterService.getByfeeId(feeId);
    }

    public String printDeposit(MedicalCardMaster medicalCardMaster) {
        User user = UserUtils.getUser();
        String printPath = "";
        List<String> list=new ArrayList<String>();
         Map<String,Object> map=new HashMap<String,Object>();
         map.put("hospital","一卡通充值单据");
         map.put("name",medicalCardMaster.getName());//患者姓名
         map.put("userName",user.getName());
         map.put("money",medicalCardMaster.getDeposit());//
         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
         map.put("time",sdf.format(new java.util.Date()));
         map.put("balance",medicalCardMaster.getBalance());
         printPath = PrintPdfUtils.previewPdf(user.getId(), "his/card/cardDeposit.jasper", map, null, "cardDeposit");
         return printPath;
    }

    /**
     * 发药打印
     * @param medicalCardMaster
     * @return
     */
    public String printItem(MedicalCardMaster medicalCardMaster) {
        User user = UserUtils.getUser();
        String printPath = "";
        Map<String,Object> map=new HashMap<String,Object>();
        MedicalCardMaster med=this.findByCardNo(medicalCardMaster);
        map.put("name",med.getName());//患者姓名
        map.put("balance",med.getBalance());
//        String hospital = companyDao.get(user.getOrgId()).getOrgName();
        map.put("hospital","一卡通消费单据");
        map.put("userName",user.getName());
        map.put("money",medicalCardMaster.getConsumeMoney());//
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        map.put("time",sdf.format(new java.util.Date()));
        printPath = PrintPdfUtils.previewPdf(user.getId(), "his/card/cardItem.jasper", map, null, "cardItem");
        return printPath;

    }
}